<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="消息标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入消息标题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="消息模板" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="templateCode">
              <j-dict-select-tag type="list" v-model="model.templateCode" dictCode="sys_sms_template,template_name,template_code" placeholder="请选择消息模板" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="接收人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="user">
              <j-select-user-by-dep v-model="model.user" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="接收角色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="part">
              <j-popup
                v-model="model.partname"
                field="partname"
                org-fields="id,role_name"
                dest-fields="part,partname"
                code="messagePart"
                :multi="true"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="接收角色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="part" v-show="false">
             <input v-model="model.part"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="部门分配" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="department">
              <j-select-depart v-model="model.department" :multi="true"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优先级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="level">
              <j-dict-select-tag type="list" v-model="model.level" dictCode="message_level" placeholder="请选择优先级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="消息类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="message_type" placeholder="请选择消息类型" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { queryall } from '@api/api'
  import { NbcioListMixin } from '@/mixins/NbcioListMixin'
  export default {
    name: 'SysMessageSettingForm',
    mixins:[NbcioListMixin],
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          part:"",
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           title: [
              { required: true, message: '请输入消息标题!'},
           ],
           templateCode: [
              { required: true, message: '请输入消息模板!'},
           ],
        },
        url: {
          list: "/message/sysMessageSetting/list",
          add: "/message/sysMessageSetting/add",
          edit: "/message/sysMessageSetting/edit",
          queryById: "/message/sysMessageSetting/queryById"
        },
        rolesOptions:[],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initRoleList()
    },
    methods: {
      initRoleList(){
        queryall().then((res)=>{
          if(res.success){
            this.rolesOptions = res.result.map((item,index,arr)=>{
              let c = {label:item.roleName, value:item.id}
              return c;
            })
            console.log('this.rolesOptions: ',this.rolesOptions)
          }
        });
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      popupCallback(value,row){
        this.model = Object.assign(this.model, row);
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>